// import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Http,Response,Headers,RequestOptions} from '@angular/http';
// import { Storage } from "@ionic/storage";
import { GlobalData } from '../globalData';
 
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
 
 
/*
  Generated class for the RestProvider provider.
  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class ApiProvider {
  public headers: any;
  constructor(public http: Http,
              // public storage: Storage,
              public globalData: GlobalData) {
    // console.log('Hello RestProvider Provider');
  }
  public baseURL = "http://120.78.177.95:8080";//环境
  // 上传图片
  public uploadFileToOssFront = `${this.baseURL}/szgjadmin/base/idcardCheck`//正面
  public uploadFileToOssBack = `${this.baseURL}/szgjadmin/base/uploadFileToOss`//背面
  
  // 获取今日推荐区域数据
  private getTodayRecommedUrl = `${this.baseURL}/szgjadmin/app/product/getNewTodayRecommed`
  // 获取显示/隐藏的配置
  private getAppShowConfigUrl = `${this.baseURL}/szgjadmin/app/appconfig/getAppShowConfig`
  // 提交实名认证身份资料
  private saveRealInfoUrl = `${this.baseURL}/szgjadmin/app/appuser/saveRealInfo`
  // 发送验证码 post query:{telephone: XXX}
  private sendSmsCodeUrl = `${this.baseURL}/szgjadmin/app/appuser/appSmsCode`
  // 登录 post query:{telephone:xx, smsCode:xxx}
  private loginBySmsCodeUrl = `${this.baseURL}/szgjadmin/app/appuser/loginBySmsCode`
  // 根据手机号码设置用户密码 post query:{telephone:xx, password:xxx}
  private setPasswordUrl = `${this.baseURL}/szgjadmin/app/appuser/setPassword`
  // 重置密码 post body:{telephone:xxx}
  private resetPasswordUrl = `${this.baseURL}/szgjadmin/app/appuser/resetPassword`
  // 完善用户信息 post body:{wechatNum:xxx, telephone:xxx, zmScore:xxx, realName:xxx, idcard:xxx}
  private updateAppUserInfoUrl = `${this.baseURL}/szgjadmin/app/appuser/updateAppUserInfo`
  // 获取用户信息 post body:{telephone:xxx}
  private getAppUserInfoUrl = `${this.baseURL}/szgjadmin/app/appuser/getAppUserInfo`
  // 开启画面弹窗
  private getWindowProductUrl = `${this.baseURL}/szgjadmin/app/product/getWindowProductNew`
  // 为您推荐
  private getProductListUrl = `${this.baseURL}/szgjadmin/app/product/getRecommed`
  private userClickProductUrl = `${this.baseURL}/szgjadmin/app/product/userClickProduct`
  // 贷款大全
  private getProductListNewUrl = `${this.baseURL}/szgjadmin/app/product/getDkdqList`
  // 获取产品详情 post body:{productId:1}
  private getProductDetailUrl = `${this.baseURL}/szgjadmin/app/product/getProductDetail`
  // 签到 post body:{appUserId:1}
  private userSignUrl = `${this.baseURL}/szgjadmin/app/appSign/userSign`
  // 米分及版本号
  private getNewAppVersionUrl = `${this.baseURL}/szgjadmin/app/appconfig/getNewAppVersion`
  // 新增投诉信息
  private addComplaintUrl = `${this.baseURL}/szgjadmin/app/appuser/addComplaint`
  // 秒批口子
  private getMiaoPiUrl = `${this.baseURL}/szgjadmin/app/product/getTwoDistrictProductNew`
  // 签到记录接口
  private getUserSignInfoUrl = `${this.baseURL}/szgjadmin/app/appSign/getUserSignInfo`
  // 抢红包
  private userRedPacketUrl = `${this.baseURL}/szgjadmin/app/redpack/userRedPacket`
  // 跑马灯
  private getProductHeadlistUrl = `${this.baseURL}/szgjadmin/app/product/getProductHeadlist`
  //埋点
  
  private addEventUrl = `http://47.56.80.206:8080/record/event/addEvent`//环境
  //埋点
  addEvent(data):Observable<string[]>{
    return this.getUrlPostReturn(this.addEventUrl, data)
  }
  // 获取今日推荐区域
  getTodayRecommed(data):Observable<string[]>{
      return this.getUrlPostReturn(this.getTodayRecommedUrl, data)
  }
  // 获取显示/隐藏的配置
  getAppShowConfig(data):Observable<string[]>{
    return this.getUrlPostReturn(this.getAppShowConfigUrl, data)
  }
  // 获取首页弹窗数据
  getWindowProduct(data):Observable<string[]>{
    return this.getUrlPostReturn(this.getWindowProductUrl, data)
  }
  // 发送验证码
  sendSmsCode(data):Observable<string[]>{
    return this.getUrlPostReturn(this.sendSmsCodeUrl, data)
  }
  // 登录
  loginBySmsCode(data):Observable<string[]>{
     return this.getUrlPostReturn(this.loginBySmsCodeUrl, data)
  }
  //设置用户密码
  setPassword(data):Observable<string[]>{
    return this.getUrlPostReturn(this.setPasswordUrl, data)
  }
  //重置密码
  resetPassword(data):Observable<string[]>{
    return this.getUrlPostReturn(this.resetPasswordUrl, data)
  }
  //完善用户信息
  updateAppUserInfo(data):Observable<string[]>{
    return this.getUrlPostReturn(this.updateAppUserInfoUrl, data)
  }
  //获取用户信息
  getAppUserInfo(data):Observable<string[]>{
    return this.getUrlPostReturn(this.getAppUserInfoUrl, data)
  }
  // 为您推荐
  getProductList(data):Observable<string[]>{
    return this.getUrlPostReturn(this.getProductListUrl, data)
  }
  userClickProduct(data):Observable<string[]>{
    return this.getUrlPostReturn(this.userClickProductUrl, data)
  }
  // 贷款大全
  getProductListNew(data):Observable<string[]>{
    return this.getUrlPostReturn(this.getProductListNewUrl, data)
  }
  // 获取米分及版本号信息
  getNewAppVersion(data):Observable<string[]>{
    return this.getUrlPostReturn(this.getNewAppVersionUrl, data)
  }
  //获取产品详情
  getProductDetail(data):Observable<string[]>{
    return this.getUrlPostReturn(this.getProductDetailUrl, data)
  }
  //签到
  userSign(data):Observable<string[]>{
    return this.getUrlPostReturn(this.userSignUrl, data)
  }
  //获取签到记录
  getUserSignInfo(data):Observable<string[]>{
    return this.getUrlPostReturn(this.getUserSignInfoUrl, data)
  }
  // 抢红包
  userRedPacket(data):Observable<string[]>{
    return this.getUrlPostReturn(this.userRedPacketUrl, data)
  }
  // 新增投诉信息
  addComplaint(data):Observable<string[]>{
    return this.getUrlPostReturn(this.addComplaintUrl, data)
  }
  // 获取秒批口子
  getMiaoPi(data):Observable<string[]>{
    return this.getUrlPostReturn(this.getMiaoPiUrl, data)
  }
  // 提交实名认证身份资料
  saveRealInfo(data): Observable<string[]>{
    return this.getUrlPostReturn(this.saveRealInfoUrl, data)
  }
  // 跑马灯
  getProductHeadlist(data):Observable<string[]>{
    return this.getUrlPostReturn(this.getProductHeadlistUrl, data)
  }
  
  // get请求
  // private getUrlReturn(url:string, config:any={}):Observable<string[]>{
  //   return this.http.get(url, config)
  //                   .map(this.extractDate)
  //                   .catch(this.handleError);
  // }
  // post请求
  private getUrlPostReturn(url:string, data:any={}):Observable<string[]>{
    let header = new Headers({
      'app_token': window.localStorage.getItem('phoneNum')
    });
    let options = new RequestOptions({
      headers: header
    });
    // console.log('this.headers', options)
    return this.http.post(url, data, options)
                    .map(this.extractDate)
                    .catch(this.handleError);
  }
 
  //处理接口返回的数据,处理成json格式
  private extractDate(res:Response){
    let body = res.json();
    return body || {};
  }
 
  // //处理请求中的错误，考虑了各种情况的错误处理并在console.log中显示error
  private handleError(error:Response | any){
    console.log('错误处理err',  error, error.status)
    if(error.status == 401 || error.status == 500){
      return Promise.reject(error['status']);
    }
    let errMsg:string;
    if(error instanceof Response){
      const err = error || '';
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    }else{
      errMsg = error.message?error.message:error.toString()
    }
    console.log('errorMsg', errMsg);
    return Promise.reject(errMsg);
  }
}
